package com.baizhi.springboot.controller;

import com.baizhi.springboot.entity.Label;
import com.baizhi.springboot.entity.Schedule;
import com.baizhi.springboot.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ScheduleController {
    @Autowired
    private ScheduleService scheduleService;

    @RequestMapping("/selectScheduleByPage")
    public Map selectScheduleByPage(Integer page,Integer limit) {
        return scheduleService.selectScheduleByPage(page,limit);
    }
    @RequestMapping("/insertSchedule")
    public Map insertSchedule(Schedule schedule) {

        HashMap<Object, Object> map = new HashMap<>();

        try {
            scheduleService.insertSchedule(schedule);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/deleteSchedule")
    public Map  deleteSchedule(Integer id){
        HashMap<Object, Object> map = new HashMap<>();

        try {
            scheduleService.deleteSchedule(id);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @RequestMapping("/selectOneSchedule")
    public Map selectOneSchedule(Integer id, HttpSession session) {
        Schedule schedule= scheduleService.selectOneSchedule(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("schedule", schedule);
        return map;
    }

    @RequestMapping("/updateSchedule")
    public Map updateSchedule(Schedule schedule) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
           // System.out.println(label);
            scheduleService.updateSchedule(schedule);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }

}
